package io.sdph.controller;

import io.sdph.userprofile.ISessionManager;
import io.sdph.userprofile.model.User;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


/**
 * Servlet odpowiedzialny za obs�u�enie logowania.
 *
 * @author Konrad Szewczyk
 * @since 0.0.1
 */
@Controller
@RequestMapping("/login")
public class LoginForm {
       
        private static final String INPUT_LOGIN = "login";
        private static final String INPUT_PASSWORD = "password";
        private static final String ANOTHER = "all";

        private static final String OUTPUT_ERROR = "errors";
        private static final String LOGIN_EMPTY_ERROR = "login.login.error.empty";
        private static final String PASSWORD_EMPTY_ERROR = "login.password.error.empty";
        private static final String PASSWORD_OR_LOGIN_INVALID_ERROR = "login.password-or-login.error.invalid";
        private static final String ALREADY_LOGGED_IN = "login.error.alreadyLoggedIn";
       
        private static final String PAGE_VIEW = "LoginFormPage";

        /** Komponent pomocniczy do zarz�dzania sesj� u�ytkownika. */
        @Autowired
        ISessionManager sessionManager;
       
        //HttpServletRequest arg0, HttpServletResponse arg1
        @RequestMapping(method=RequestMethod.GET)
        public ModelAndView showPage() throws Exception {
               
                ModelAndView model = new ModelAndView(PAGE_VIEW);
                model.addObject("pageName", "login");
 
                return model;
        }
       
        @RequestMapping(method=RequestMethod.POST)
        public ModelAndView login(
                        @RequestParam(INPUT_LOGIN) final String login,
                        @RequestParam(INPUT_PASSWORD) final String password ) {
               
               
                boolean hasErrors = true;
                Map<String, String> errors = new HashMap();
               
                if (StringUtils.isEmpty(login)) {
                        errors.put(INPUT_LOGIN, LOGIN_EMPTY_ERROR);
                        hasErrors = false;
                }
                if (StringUtils.isEmpty(password)) {
                        errors.put(INPUT_PASSWORD, PASSWORD_EMPTY_ERROR);
                        hasErrors = false;
                }

                if (hasErrors) {
                        User user = sessionManager.getCurrentUser();
                        if (user == null) {
                                if (!sessionManager.logIn(login, password)) {
                                        errors.put(ANOTHER, PASSWORD_OR_LOGIN_INVALID_ERROR);
                                }
                        } else {
                                errors.put(ANOTHER, ALREADY_LOGGED_IN);
                        }
                }
                ModelAndView model = null;
                if(errors.isEmpty()) {
                	model = new ModelAndView("admin");
                	model.addObject("pageName", "products");
                } else {
                	model = new ModelAndView(PAGE_VIEW);
                }
                
                model.addObject(OUTPUT_ERROR, errors);
                model.addObject("pageName", "login");

                return model;
        }
        
        @RequestMapping(method=RequestMethod.POST)
        public ModelAndView logout() {
        	ModelAndView model = new ModelAndView(PAGE_VIEW);
        	
        	sessionManager.logOut();
        	
        	model.addObject("pageName", "login");
        	return model;
        }
       
}
